package com.eltsistemas.controller.grupo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.eltsistemas.dao.BuscaPermissao;
import com.eltsistemas.dao.GenericDao;
import com.eltsistemas.hibernate.HibernateUtil;
import com.eltsistemas.modelo.Grupo;
import com.eltsistemas.modelo.GrupoPermissao;
import com.eltsistemas.modelo.Permissao;

public class GrupoComPermissao {
	
	public Map<GrupoPermissao,List<GrupoPermissao>> tratarParametros( Map<String,String[]> param )
	{
		Map<GrupoPermissao,List<GrupoPermissao>> resultado = new HashMap<>();
		
		for( Entry<String,String[]> entry : param.entrySet() )
		{
			if( entry.getKey().toString().indexOf( "_" ) <= 0 )
			{
				continue ;
			}
			
			String[] partes = entry.getKey().toString().split( "_" );
		
			GrupoPermissao gp = new GrupoPermissao();
			
			gp.setModulo(partes[0]);
			
			Permissao p = new Permissao();
			p.setId( getPermissaoById( partes[1] ));
			
			gp.setPermissao(p);
			
			if( !resultado.containsKey( gp ) )
			{
				resultado.put( gp , new ArrayList<GrupoPermissao>() );
			}
			
			resultado.get( gp ).add( gp );
		}
		
		return resultado;
	}

	public Map<String,Set<String>> getPermissao2(Grupo grupo) throws HibernateException
	{
		Session session = new HibernateUtil().getSession();
		session.beginTransaction();
		
		Map<String, Set<String>> reordenadoMap = new HashMap<>();
		Map<String, List<String>> param = new HashMap<>();
	
		GenericDao<Permissao> daoPermissao = new GenericDao<>(session, Permissao.class);
		for(Permissao p: daoPermissao.listAllByAsc() )
		{
			
			if(!param.containsKey( String.valueOf( p.getId() ) ))
			{
				
				List<GrupoPermissao> gpList = new BuscaPermissao(session).getGrupoPermissao(grupo, p.getId());
				List<String> valorList = new ArrayList<>();
				for(GrupoPermissao gp : gpList)
				{
					valorList.add(gp.getModulo());
				}
				param.put( String.valueOf( p.getId() ) , valorList );
			}
			
		}
		
		Map<String,Set<String>> map = new HashMap<>();
		for( Entry<String, List<String>> entry : param.entrySet() )
		{
			
			map.put( String.valueOf( entry.getKey() ), new HashSet<String>() );
			
			for( String val : entry.getValue() )
			{
				if( val != null )
				{
					map.get( entry.getKey() ).add( val.toLowerCase() );
				}
			}
		}
		
		List<String> chaveList = new LinkedList<>( map.keySet() );
		Collections.sort( chaveList );
		
		for(  String key : chaveList )
		{
			reordenadoMap.put( key , map.get( key ) );
		}
		session.close();
		return reordenadoMap;
	}
	
	public List<String> getPermissao(Grupo grupo, String nomePermissao) throws HibernateException
	{
		Session session = new HibernateUtil().getSession();
		session.beginTransaction();
		
		List<GrupoPermissao> gpListAll = new BuscaPermissao(session).getGrupoPermissao(grupo, this.getPermissaoById(nomePermissao) );
		List<String> moduloList = new ArrayList<>();
		
		for(GrupoPermissao gp : gpListAll)
		{
			if( gp.getPermissao().getId().equals( this.getPermissaoById(nomePermissao) ) )
			{
				moduloList.add(gp.getModulo());
			}
		}
		
		session.close();
		Collections.sort( moduloList);
		return moduloList;
	}
	
	public Long getPermissaoById(String nome) throws HibernateException
	{
		Session session = new HibernateUtil().getSession();
		session.beginTransaction();
		
		GenericDao<Permissao> daoPermissao = new GenericDao<>(session, Permissao.class);
		Permissao p = new Permissao();
		p = daoPermissao.findByName(nome);
		
		session.close();
		return p.getId();
	}
	
	public String getPermissaoByName(Long id) throws HibernateException
	{
		Session session = new HibernateUtil().getSession();
		session.beginTransaction();
		
		GenericDao<Permissao> daoPermissao = new GenericDao<>(session, Permissao.class);
		Permissao p = new Permissao();
		p = daoPermissao.findById(id);
		
		session.close();
		return p.getNome();
	}
}
